<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var threeSum = function (nums) {
        nums.sort((a, b) => a - b)
        let res = []
        for (let i = 0; i < nums.length; i++) {
          if (nums[i] == nums[i - 1]) continue
          let left = i + 1
          let right = nums.length - 1
          while (left < right) {
            let sum = nums[i] + nums[left] + nums[right]
            if (sum == 0) {
              while (nums[left] == nums[left + 1]) {
                left++
              }
              while (nums[right] == nums[right - 1]) {
                right--
              }
              res.push([nums[i], nums[left], nums[right]])
              left++
              right--
            } else if (sum < 0) {
              left++
            } else {
              right--
            }
          }
        }
        return res
      }
    </script>
  </body>
</html>
